//package sjd.ahsj.springbootmybatisplusoracle.config;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.json.JSONObject;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import sjd.ahsj.springbootmybatisplusoracle.jwt.TokenAuthenticationService;
//import sjd.ahsj.springbootmybatisplusoracle.model.AccountCredentials;
//import sjd.ahsj.springbootmybatisplusoracle.model.JSONResult;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * Created by Eastern unbeaten on 2018/4/1.
// */
//public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {
//
//    public JWTLoginFilter(String url, AuthenticationManager authManager) {
//        super(new AntPathRequestMatcher(url));
//        setAuthenticationManager(authManager);
//    }
//
//    @Override
//    public Authentication attemptAuthentication(
//            HttpServletRequest req, HttpServletResponse res)
//            throws AuthenticationException, IOException, ServletException {
//
//        // JSON反序列化成 AccountCredentials
//        AccountCredentials creds = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class);
//        System.out.println(req.getParameter("username"));
//        System.out.println(req.getParameter("password"));
////        AccountCredentials acc = new AccountCredentials();
////        acc.setUsername(req.getParameter("username"));
////        acc.setPassword(req.getParameter("password"));
//        // 返回一个验证令牌
//        return getAuthenticationManager().authenticate(
//                new UsernamePasswordAuthenticationToken(
//                        creds.getUsername(),
//                        creds.getPassword()
//                )
//        );
//    }
//
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult)
//            throws IOException, ServletException {
//        TokenAuthenticationService.addAuthentication(response, authResult.getName());
//    }
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
//        response.setContentType("application/json");
//        response.setStatus(HttpServletResponse.SC_OK);
//        response.getOutputStream().println(JSONResult.fillResultString(500, "Internal Server Error!!!", JSONObject.NULL));
//    }
//}
